<?php

namespace app\common\model;

use basic\ModelBasic;

class Article extends ModelBasic
{
	use \traits\ModelTrait;

    //protected $table = 'eb_user';

	/**
	 * 设置where条件
	 * @Author   FW
	 * @DateTime 2021-01-12
	 * @Email    123763712@qq.com
	 * @param    [type]           $where [description]
	 * @param    string           $alias [description]
	 * @param    [type]           $model [description]
	 */
	public static function setWhere($where,$alias='m',$model=null){
        $model = $model===null ? new self() : $model;

        $model->alias($alias);

        if ( isset($where['cate_id']) && $where['cate_id'] > 0 ) {
            $model->where($alias.'.cate_id',$where['cate_id']);
        }

        if ( isset($where['status']) && $where['status'] > 0 ) {
            $model->where($alias.'.status', $where['status'] );
        }

        $model->order($alias.'.version DESC');
        return $model;
    }

    /**
     * 列表
     * @Author   FW
     * @DateTime 2020-09-24
     * @Email    123763712@qq.com
     * @param    [type]
     * @return   [type]
     */
    public static function getList($where){
    	$count = self::setWhere($where)->count();
    	$data  = [];
    	if ( $count == 0 ) {
    		return compact('data','count');
    	}

        $data=self::setWhere($where,'m')
        ->field('m.id,m.title,m.status,m.sort,m.cate_id,m.version,c.cate_name')
        ->join('__ARTICLE_CATEGORY__ c' ,'m.cate_id = c.id' , 'left')
        ->page($where['page'] ?? 1, $where['limit'] ?? 10)
        ->select();

        if ( !empty($data) ) {
        	$data = $data->toArray();
        	$data = static::process_list($data);
        }

        return compact('data','count');
    }

    /**
     * 处理列表数据
     * @Author   FW
     * @DateTime 2021-01-12
     * @Email    123763712@qq.com
     * @param    array            $list [description]
     * @return   [type]                 [description]
     */
    public static function process_list (array $list)
    {
        return $list;
    }

    /**
     * [getOne description]
     * @Author   FW
     * @DateTime 2021-01-12
     * @Email    123763712@qq.com
     * @param    [type]           $id [description]
     * @return   [type]               [description]
     */
    public static function getOne ($id)
    {
        $item = self::alias('m')->where('m.id',$id)
        	->field('m.*,c.cate_name')
            ->join('__ARTICLE_CATEGORY__ c' ,'m.cate_id = c.id' , 'left')
            ->find();

        if ( empty($item) ) {
            return [];
        }
        return $item;
    }
} //Class End
